package com.sj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sj.entity.Category;
import com.sj.mapper.CategoryMapper;
import com.sj.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 小七
 * @since 2021-07-30
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public IPage<Category> listCategoryByPage(Integer userId, Integer current, Integer size) {
        IPage<Category> page = new Page<Category>(current, size);
        IPage<Category> categoryPageInfo = null;
        if (userId == null) {
            categoryPageInfo = categoryMapper.selectPage(page, new QueryWrapper<Category>().groupBy("category_name"));
        } else {
            categoryPageInfo = categoryMapper.selectPage(page, new QueryWrapper<Category>().eq("user_id", userId));
        }
        return categoryPageInfo;
    }
}
